home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHAP09.TXT < prev    next >
Text File  |  1989-12-30  |  33KB  |  719 lines

  1.                      Chapter 9 - Standard Input/Output
  2.  
  3.  
  4.                           THE STDIO.H HEADER FILE
  5.  
  6.              Load  the file SIMPLEIO.C for our first look at a  file 
  7.         with  standard I/O.   Standard I/O refers to the most  usual 
  8.         places  where  data is either read from,  the  keyboard,  or 
  9.         written to, the video monitor.  Since they are used so much, 
  10.         they are used as the default I/O devices and do not need  to 
  11.         be  named in the Input/Output instructions.   This will make 
  12.         more  sense when we actually start to use them so lets  look 
  13.         at the file in front of you.
  14.  
  15.              The  first thing you will notice is the first  line  of 
  16.         the  file,  the #include "stdio.h" line.   This is very much 
  17.         like  the  #define  we have  already  studied,  except  that 
  18.         instead of a simple substitution,  an entire file is read in 
  19.         at  this  point.   The  system  will  find  the  file  named 
  20.         "stdio.h"  and read its entire contents in,  replacing  this 
  21.         statement.   Obviously then,  the file named "stdio.h"  must 
  22.         contain  valid  C source statements that can be compiled  as 
  23.         part  of  a program.   This particular file is  composed  of 
  24.         several standard #defines to define some of the standard I/O 
  25.         operations.   The file is called a header file and you  will 
  26.         find several different header files on the source disks that 
  27.         came  with  your compiler.   Each of the header files has  a 
  28.         specific  purpose and any or all of them can be included  in 
  29.         any program.
  30.  
  31.              Most C compilers use the double quote marks to indicate 
  32.         that  the  "include"  file  will be  found  in  the  current 
  33.         directory.   A  few use the "less than" and  "greater  than" 
  34.         signs  to indicate that the file will be found in a standard 
  35.         header  file.   Nearly all MSDOS C compilers use the  double 
  36.         quotes,  and  most require the "include" file to be  in  the 
  37.         default  directory.   All  of the programs in this  tutorial 
  38.         have the double quotes in the "include" statements.  If your 
  39.         compiler  uses the other notation,  you will have to  change 
  40.         them before compiling.
  41.  
  42.                         INPUT/OUTPUT OPERATIONS IN C
  43.  
  44.              Actually  the  C programming language has no  input  or 
  45.         output operations defined as part of the language, they must 
  46.         be user defined.   Since everybody does not want to reinvent 
  47.         his  own input and output operations,  the compiler  writers 
  48.         have done a lot of this for us and supplied us with  several 
  49.         input  functions and several output functions to aid in  our 
  50.         program development.   The functions have become a standard, 
  51.         and  you  will find the same functions available  in  nearly 
  52.         every  compiler.   In fact,  the industry standard of the  C 
  53.         language  definition has become the book written by Kernigan 
  54.         and Ritchie, and they have included these functions in their 
  55.  
  56.  
  57.                                   Page 55
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                      Chapter 9 - Standard Input/Output
  68.  
  69.  
  70.         definition.   You will often,  when reading literature about 
  71.         C,  find  a  reference to K & R.   This refers to  the  book 
  72.         written  by Kernigan and Ritchie.   You would be advised  to 
  73.         purchase a copy for reference.
  74.  
  75.              You should print out the file named "stdio.h" and spend 
  76.         some  time studying it.   There will be a lot that you  will 
  77.         not understand about it, but parts of it will look familiar.  
  78.         The  name  "stdio.h"  is  sort  of  cryptic  for   "standard 
  79.         input/output header",  because that is exactly what it does.  
  80.         It  defines  the standard input and output functions in  the 
  81.         form of #defines and macros.  Don't worry too much about the 
  82.         details  of this now.   You can always return to this  topic 
  83.         later  for  more study if it interests  you,  but  you  will 
  84.         really  have no need to completely understand the  "stdio.h" 
  85.         file.  You will have a tremendous need to use it however, so 
  86.         these comments on its use and purpose are necessary.
  87.  
  88.                             OTHER INCLUDE FILES
  89.  
  90.              When  you  begin writing larger programs and  splitting 
  91.         them  up into separately compiled portions,  you  will  have 
  92.         occasion  to  use  some  statements common to  each  of  the 
  93.         portions.   It would be to your advantage to make a separate 
  94.         file  containing  the  statements and use  the  #include  to 
  95.         insert it into each of the files.  If you want to change any 
  96.         of the common statements,  you will only need to change  one 
  97.         file  and  you will be assured of having all of  the  common 
  98.         statements  agree.   This  is  getting  a  little  ahead  of 
  99.         ourselves  but  you  now  have  an  idea  how  the  #include 
  100.         directive can be used.
  101.  
  102.                     BACK TO THE FILE NAMED "SIMPLEIO.C"
  103.              
  104.              Lets  continue our tour of the file in  question.   The 
  105.         one  variable  "c" is defined and a message is  printed  out 
  106.         with the familiar "printf" function.  We then find ourselves 
  107.         in  a continuous loop as long as "c" is not equal to capital 
  108.         X.   If  there is any question in your mind about  the  loop 
  109.         control, you should review chapter 3 before continuing.  The 
  110.         two  new functions within the loop are of paramount interest 
  111.         in this program since they are the new functions.  These are 
  112.         functions to read a character from the keyboard and  display 
  113.         it on the monitor one character at a time.
  114.  
  115.              The  function "getchar()" reads a single character from 
  116.         the  standard  input  device,  the  keyboard  being  assumed 
  117.         because that is the standard input device, and assigns it to 
  118.         the variable "c".   The next function "putchar(c)", uses the 
  119.         standard output device,  the video monitor,  and outputs the 
  120.         character contained in the variable "c".   The character  is 
  121.  
  122.  
  123.                                   Page 56
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                      Chapter 9 - Standard Input/Output
  134.  
  135.  
  136.         output  at  the  current cursor location and the  cursor  is 
  137.         advanced  one space for the next character.   The system  is 
  138.         therefore taking care of a lot of the overhead for us.   The 
  139.         loop  continues reading and displaying characters  until  we 
  140.         type a capital X which terminates the loop. 
  141.  
  142.              Compile and run this program for a few surprises.  When 
  143.         you type on the keyboard, you will notice that what you type 
  144.         is displayed faithfully on the screen,  and when you hit the 
  145.         return key,  the entire line is repeated.   In fact, we only 
  146.         told  it  to output each character once but it seems  to  be 
  147.         saving  the  characters up and redisplaying them.   A  short 
  148.         explanation is in order.
  149.  
  150.                DOS IS HELPING US OUT (OR GETTING IN THE WAY)
  151.  
  152.              We need to understand a little bit about how DOS  works 
  153.         to  understand  what is happening here.   When data is  read 
  154.         from  the keyboard,  under DOS control,  the characters  are 
  155.         stored  in  a buffer until a carriage return is  entered  at 
  156.         which  time the entire string of characters is given to  the 
  157.         program.   When the characters are being typed, however, the 
  158.         characters are displayed one at a time on the monitor.  This 
  159.         is called echo,  and happens in many of the applications you 
  160.         run. 
  161.  
  162.              With  the above paragraph in mind,  it should be  clear 
  163.         that when you are typing a line of data into "SIMPLEIO", the 
  164.         characters are being echoed by DOS,  and when you return the 
  165.         carriage,  the characters are given to the program.  As each 
  166.         character  is given to the program,  it displays it  on  the 
  167.         screen  resulting  in  a repeat of the line  typed  in.   To 
  168.         better  illustrate  this,  type  a line  with  a  capital  X 
  169.         somewhere  in the middle of the line.   You can type as many 
  170.         characters  as you like following the "X" and they will  all 
  171.         display because the characters are being read in under  DOS, 
  172.         echoed  to the monitor,  and placed in the DOS input buffer.  
  173.         DOS doesn't think there is anything special about a  capital 
  174.         X.   When the string is given to the program,  however,  the 
  175.         characters  are  accepted by the program one at a  time  and 
  176.         sent  to  the monitor one at a time,  until a capital  X  is 
  177.         encountered.   After the capital X is displayed, the loop is 
  178.         terminated,  and the program is terminated.   The characters 
  179.         on  the input line following the capital X are not displayed 
  180.         because the capital X signalled program termination.
  181.  
  182.              Compile  and  run  "SIMPLEIO.C".    After  running  the 
  183.         program  several  times  and  feeling  confidant  that   you 
  184.         understand  the above explanation,  we will go on to another 
  185.         program.
  186.  
  187.  
  188.  
  189.                                   Page 57
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                      Chapter 9 - Standard Input/Output
  200.  
  201.  
  202.              Don't  get  discouraged by the  above  seemingly  weird 
  203.         behavior  of the I/O system.   It is strange,  but there are 
  204.         other ways to get data into the computer.  You will actually 
  205.         find the above method useful for many applications,  and you 
  206.         will probably find some of the following useful also.
  207.  
  208.                          ANOTHER STRANGE I/O METHOD
  209.  
  210.              Load  the file named SINGLEIO.C and display it on  your 
  211.         monitor for another method of character I/O.  Once again, we 
  212.         start  with  the  standard I/O  header  file,  we  define  a 
  213.         variable named "c",  and we print a welcoming message.  Like 
  214.         the  last  program,  we are in a loop that will continue  to 
  215.         execute  until  we type a capital X,  but the  action  is  a 
  216.         little different here.
  217.  
  218.              The  "getch()"  is  a  new  function  that  is  a  "get 
  219.         character" function.  It differs from "getchar()" in that it 
  220.         does  not  get tied up in DOS.   It reads the  character  in 
  221.         without echo, and puts it directly into the program where it 
  222.         is  operated  on immediately.   This function then  reads  a 
  223.         character,  immediately  displays  it  on  the  screen,  and 
  224.         continues the operation until a capital X is typed.
  225.  
  226.              When  you compile and run this program,  you will  find 
  227.         that there is no repeat of the lines when you hit a carriage 
  228.         return,  and  when  you  hit  the  capital  X,  the  program 
  229.         terminates immediately.  No carriage return is needed to get 
  230.         it to accept the line with the X in it.   We do have another 
  231.         problem here, there is no linefeed with the carriage return.
  232.  
  233.                           NOW WE NEED A LINE FEED
  234.  
  235.              It  is not apparent to you in most application programs 
  236.         but  when  you hit the enter key,  the  program  supplies  a 
  237.         linefeed to go with the carriage return.  You need to return 
  238.         to  the  left side of the monitor and you also need to  drop 
  239.         down  a line.   The linefeed is not automatic.  We  need  to 
  240.         improve  our program to do this also.   If you will load and 
  241.         display the program named BETTERIN.C, you will find a change 
  242.         to incorporate this feature.
  243.  
  244.              In BETTERIN.C, we have two additional statements at the 
  245.         beginning  that  will  define the character  codes  for  the 
  246.         linefeed (LF), and the carriage return (CR).  If you look at 
  247.         any  ASCII table you will find that the codes 10 and 13  are 
  248.         exactly  as  defined  here.   In  the  main  program,  after 
  249.         outputting the character,  we compare it to CR, and if it is 
  250.         equal to CR,  we also output a linefeed which is the LF.  We 
  251.         could  have  just  as well have left  out  the  two  #define 
  252.         statements and used "if (c == 13) putchar(10);" but it would 
  253.  
  254.  
  255.                                   Page 58
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                      Chapter 9 - Standard Input/Output
  266.  
  267.  
  268.         not  be  very descriptive of what we are  doing  here.   The 
  269.         method  used  in the program represents  better  programming 
  270.         practice.
  271.  
  272.              Compile  and  run BETTERIN.C to see if it does what  we 
  273.         have said it should do.   It should display exactly what you 
  274.         type in, including a linefeed with each carriage return, and 
  275.         should stop immediately when you type a capital X. 
  276.  
  277.              If  you are using a nonstandard compiler,  it  may  not 
  278.         find  a "CR" because your system returns a "LF" character to 
  279.         indicate  end-of-line.   It will be up to you  to  determine 
  280.         what method your compiler uses.   The quickest way is to add 
  281.         a  "printf"  statement  that prints the input  character  in 
  282.         decimal format.
  283.  
  284.                            WHICH METHOD IS BEST?
  285.  
  286.              We have examined two methods of reading characters into 
  287.         a  C program,  and are faced with a choice of which  one  we 
  288.         should  use.   It really depends on the application  because 
  289.         each  method has advantages and disadvantages.   Lets take a 
  290.         look at each. 
  291.  
  292.              When using the first method,  DOS is actually doing all 
  293.         of  the  work for us by storing the characters in  an  input 
  294.         buffer and signalling us when a full line has been  entered.  
  295.         We  could write a program that,  for example,  did a lot  of 
  296.         calculations,  then  went to get some input.   While we were 
  297.         doing the calculations,  DOS would be accumulating a line of 
  298.         characters  for  us,  and they would be there when  we  were 
  299.         ready  for  them.   However,  we could not  read  in  single 
  300.         keystrokes   because  DOS  would  not  report  a  buffer  of 
  301.         characters to us until it recognized a carriage return.
  302.  
  303.              The second method, used in BETTERIN.C, allows us to get 
  304.         a single character,  and act on it immediately.   We do  not 
  305.         have  to  wait  until  DOS decides we can  have  a  line  of 
  306.         characters.  We cannot do anything else while we are waiting 
  307.         for  a  character  because  we are  waiting  for  the  input 
  308.         keystroke  and tying up the entire machine.   This method is 
  309.         useful  for highly interactive types of program  interfaces.  
  310.         It  is up to you as the programmer to decide which  is  best 
  311.         for your needs.
  312.  
  313.              I  should  mention at this point that there is also  an 
  314.         "ungetch" function that works with the "getch" function.  If 
  315.         you "getch" a character and find that you have gone one  too 
  316.         far,  you  can "ungetch" it back to the input device.   This 
  317.         simplifies  some  programs because you don't know  that  you 
  318.         don't  want the character until you get it.   You  can  only 
  319.  
  320.  
  321.                                   Page 59
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                      Chapter 9 - Standard Input/Output
  332.  
  333.  
  334.         "ungetch"  one character back to the input device,  but that 
  335.         is  sufficient  to  accomplish the task  this  function  was 
  336.         designed for.   It is difficult to demonstrate this function 
  337.         in  a simple program so its use will be up to you  to  study 
  338.         when you need it.
  339.  
  340.              The discussion so far in this chapter, should be a good 
  341.         indication  that,  while the C programming language is  very 
  342.         flexible,  it does put a lot of responsibility on you as the 
  343.         programmer to keep many details in mind.
  344.  
  345.                         NOW TO READ IN SOME INTEGERS
  346.  
  347.              Load  and display the file named INTIN.C for an example 
  348.         of  reading in some formatted data.   The structure of  this 
  349.         program  is  very similar to the last three except  that  we 
  350.         define  an "int" type variable and loop until  the  variable 
  351.         somehow acquires the value of 100.
  352.  
  353.              Instead of reading in a character at a time, as we have 
  354.         in the last three files,  we read in an entire integer value 
  355.         with  one  call  using the  function  named  "scanf".   This 
  356.         function  is very similar to the "printf" that you have been 
  357.         using for quite some time by now except that it is used  for 
  358.         input instead of output.   Examine the line with the "scanf" 
  359.         and  you  will notice that it does not ask for the  variable 
  360.         "valin"  directly,  but  gives the address of  the  variable 
  361.         since it expects to have a value returned from the function.  
  362.         Recall  that a function must have the address of a  variable 
  363.         in  order  to  return  the value  to  the  calling  program.  
  364.         Failing  to  supply  a pointer in the  "scanf"  function  is 
  365.         probably  the most common problem encountered in using  this 
  366.         function. 
  367.  
  368.              The  function  "scanf" scans the input  line  until  it 
  369.         finds  the first data field.   It ignores leading blanks and 
  370.         in this case,  it reads integer characters until it finds  a 
  371.         blank  or  an invalid decimal character,  at which  time  it 
  372.         stops reading and returns the value. 
  373.  
  374.              Remembering  our discussion above about the way the DOS 
  375.         input  buffer  works,  it should be clear  that  nothing  is 
  376.         actually acted on until a complete line is entered and it is 
  377.         terminated by a carriage return.   At this time,  the buffer 
  378.         is  input,  and  our  program will search  across  the  line 
  379.         reading  all  integer values it can find until the  line  is 
  380.         completely scanned.  This is because we are in a loop and we 
  381.         tell it to find a value,  print it,  find another, print it, 
  382.         etc.   If you enter several values on one line, it will read 
  383.         each one in succession and display the values.  Entering the 
  384.         value  of  100  will cause the  program  to  terminate,  and 
  385.  
  386.  
  387.                                   Page 60
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                      Chapter 9 - Standard Input/Output
  398.  
  399.  
  400.         entering  the  value 100 with other values  following,  will 
  401.         cause   termination   before  the   following   values   are 
  402.         considered. 
  403.  
  404.                       IT MAKES WRONG ANSWERS SOMETIMES
  405.  
  406.              If  you  enter a number up to and including  32767,  it 
  407.         will display correctly, but if you enter a larger number, it 
  408.         will appear to make an error.  For example, if you enter the 
  409.         value 32768,  it will display the value of -32768,  entering 
  410.         the  value  65536 will display as a  zero.   These  are  not 
  411.         errors but are caused by the way an integer is defined.  The 
  412.         most significant bit of the 16 bit pattern available for the 
  413.         integer variable is the sign bit,  so there are only 15 bits 
  414.         left  for the value.   The variable can therefore only  have 
  415.         the  values  from  -32768 to 32767,  any  other  values  are 
  416.         outside  the range of integer variables.   This is up to you 
  417.         to take care of in your programs.   It is another example of 
  418.         the increased responsibility you must assume using C  rather 
  419.         than a higher level language such as Pascal, Modula-2, etc.
  420.  
  421.              The   above  paragraph  is  true  for  most  MS-DOS   C 
  422.         compilers.   There  is  a very small possibility  that  your 
  423.         compiler uses an integer value other than 16 bits.   If that 
  424.         is  the  case,  the  same  principles will be  true  but  at 
  425.         different limits than those given above.
  426.  
  427.              Compile and run this program,  entering several numbers 
  428.         on  a line to see the results,  and with varying numbers  of 
  429.         blanks between the numbers.   Try entering numbers that  are 
  430.         too big to see what happens,  and finally enter some invalid 
  431.         characters  to  see  what the system  does  with  nondecimal 
  432.         characters.
  433.  
  434.                            CHARACTER STRING INPUT
  435.  
  436.              Load  and  display  the file named  STRINGIN.C  for  an 
  437.         example  of  reading  a string variable.   This  program  is 
  438.         identical to the last one except that instead of an  integer 
  439.         variable,  we  have defined a string variable with an  upper 
  440.         limit of 24 characters (remember that a string variable must 
  441.         have  a  null character at the end).   The variable  in  the 
  442.         "scanf"  does  not  need  an & because  "big"  is  an  array 
  443.         variable  and by definition it is already a  pointer.   This 
  444.         program  should require no additional explanation.   Compile 
  445.         and run it to see if it works the way you expect.
  446.  
  447.              You probably got a surprise when you ran it because  it 
  448.         separated  your sentence into separate words.   When used in 
  449.         the string mode of input,  "scanf" reads characters into the 
  450.         string until it comes to either the end of a line or a blank 
  451.  
  452.  
  453.                                   Page 61
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                      Chapter 9 - Standard Input/Output
  464.  
  465.  
  466.         character.   Therefore,  it  reads a word,  finds the  blank 
  467.         following it,  and displays the result.   Since we are in  a 
  468.         loop, this program continues to read words until it exhausts 
  469.         the DOS input buffer.   We have written this program to stop 
  470.         whenever  it  finds a capital X in column 1,  but since  the 
  471.         sentence  is split up into individual words,  it  will  stop 
  472.         anytime a word begins with capital X.  Try entering a 5 word 
  473.         sentence  with  a  capital X as the first character  in  the 
  474.         third word.  You should get the first three words displayed, 
  475.         and the last two simply ignored when the program stops.
  476.  
  477.              Try  entering more than 24 characters to see  what  the 
  478.         program does.  It should generate an error, but that will be 
  479.         highly dependent on the system you are using.   In an actual 
  480.         program,  it  is your responsibility to count characters and 
  481.         stop when the input buffer is full.   You may be getting the 
  482.         feeling  that a lot of responsibility is placed on you  when 
  483.         writing in C.   It is, but you also get a lot of flexibility 
  484.         in the bargain too.
  485.  
  486.                        INPUT/OUTPUT PROGRAMMING IN C
  487.  
  488.              C was not designed to be used as a language for lots of 
  489.         input and output,  but as a systems language where a lot  of 
  490.         internal operations are required.   You would do well to use 
  491.         another language for I/O intensive programming,  but C could 
  492.         be used if you desire.  The keyboard input is very flexible, 
  493.         allowing you to get at the data in a very low level way, but 
  494.         very little help is given you.  It is therefore up to you to 
  495.         take  care of all of the bookkeeping chores associated  with 
  496.         your  required  I/O operations.   This may seem like a  real 
  497.         pain in the neck, but in any given program, you only need to 
  498.         define your input routines once and then use them as needed.
  499.  
  500.              Don't let this worry you.   As you gain experience with 
  501.         C, you will easily handle your I/O requirements.
  502.  
  503.              One final point must be made about these I/O functions.  
  504.         It   is  perfectly  permissible  to  intermix  "scanf"   and 
  505.         "getchar"  functions during read operations.   In  the  same 
  506.         manner,  it  is also fine to intermix the output  functions, 
  507.         "printf" and "putchar". 
  508.  
  509.                                IN MEMORY I/O
  510.  
  511.              The  next operation may seem a little strange at first, 
  512.         but  you will probably see lots of uses for it as  you  gain 
  513.         experience.   Load the file named INMEM.C and display it for 
  514.         another  type  of I/O,  one that never accesses the  outside 
  515.         world, but stays in the computer.
  516.  
  517.  
  518.  
  519.                                   Page 62
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                      Chapter 9 - Standard Input/Output
  530.  
  531.  
  532.              In INMEM.C, we define a few variables, then assign some 
  533.         values to the ones named "numbers" for illustrative purposes 
  534.         and then use a "sprintf" function.   The function acts  just 
  535.         like  a  normal  "printf" function except  that  instead  of 
  536.         printing the line of output to a device,  it prints the line 
  537.         of  formatted  output to a character string in  memory.   In 
  538.         this  case  the string goes to the string  variable  "line", 
  539.         because  that  is the string name we inserted as  the  first 
  540.         argument  in the "sprintf" function.   The spaces after  the 
  541.         2nd  %d were put there to illustrate that the next  function 
  542.         will  search  properly  across  the  line.    We  print  the 
  543.         resulting  string and find that the output is  identical  to 
  544.         what  it would have been by using a "printf" instead of  the 
  545.         "sprintf"  in the first place.   You will see that when  you 
  546.         compile and run the program shortly.
  547.  
  548.              Since  the generated string is still in memory,  we can 
  549.         now  read  it  with the  function  "sscanf".   We  tell  the 
  550.         function  in its first argument that "line" is the string to 
  551.         use for its input,  and the remaining parts of the line  are 
  552.         exactly  what  we  would  use if we were going  to  use  the 
  553.         "scanf"  function and read data from outside  the  computer.  
  554.         Note  that it is essential that we use pointers to the  data 
  555.         because  we  want to return data from a function.   Just  to 
  556.         illustrate  that  there are many ways to declare  a  pointer 
  557.         several methods are used,  but all are pointers.   The first 
  558.         two simply declare the address of the elements of the array, 
  559.         while the last three use the fact that "result", without the 
  560.         accompanying  subscript,  is  a pointer.   Just to  keep  it 
  561.         interesting,  the  values  are read back in  reverse  order.  
  562.         Finally the values are displayed on the monitor. 
  563.  
  564.                            IS THAT REALLY USEFUL?
  565.  
  566.              It  seems sort of silly to read input data from  within 
  567.         the  computer  but  it does have  a  real  purpose.   It  is 
  568.         possible to read data in using any of the standard functions 
  569.         and  then do a format conversion in memory.   You could read 
  570.         in  a line of data,  look at a few  significant  characters, 
  571.         then  use these formatted input routines to reduce the  line 
  572.         of  data to internal representation.   That would sure  beat 
  573.         writing your own data formatting routines.
  574.  
  575.                            STANDARD ERROR OUTPUT
  576.  
  577.              Sometimes  it is desirable to redirect the output  from 
  578.         the  standard  output device to a file.   However,  you  may 
  579.         still  want the error messages to go to the standard  output 
  580.         device,  in our case the monitor.  This next function allows 
  581.         you to do that. Load and display SPECIAL.C for an example of 
  582.         this new function.
  583.  
  584.  
  585.                                   Page 63
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.                      Chapter 9 - Standard Input/Output
  596.  
  597.  
  598.  
  599.              The  program  consists  of a  loop  with  two  messages 
  600.         output,  one  to the standard output device and the other to 
  601.         the  standard  error device.   The message to  the  standard 
  602.         error  device  is  output with the  function  "fprintf"  and 
  603.         includes  the  device name "stderr" as the  first  argument.  
  604.         Other  than those two small changes,  it is the same as  our 
  605.         standard  "printf"  function.   (You will see  more  of  the 
  606.         "fprintf"  function in the next chapter,  but its  operation 
  607.         fit  in better as a part of this chapter.)  Ignore the  line 
  608.         with the "exit" for the moment, we will return to it.
  609.  
  610.              Compile  and  run this program,  and you will  find  12 
  611.         lines of output on the monitor.   To see the difference, run 
  612.         the  program  again with redirected output to a  file  named 
  613.         "STUFF" by entering the following line at the Dos prompt;
  614.  
  615.         A> special >stuff
  616.  
  617.              More  information about I/O redirection can be found in 
  618.         your  DOS manual.   This time you will only get the 6  lines 
  619.         output to the standard error device, and if you look in your 
  620.         directory,  you will find the file named "STUFF"  containing 
  621.         the other 6 lines, those to the standard output device.  You 
  622.         can use I/O redirection with any of the programs we have run 
  623.         so far,  and as you may guess, you can also read from a file 
  624.         using I/O redirection but we will study a better way to read 
  625.         from a file in the next chapter.
  626.  
  627.                      WHAT ABOUT THE exit(4) STATEMENT?
  628.  
  629.              Now  to  keep our promise about the exit(4)  statement. 
  630.         Redisplay  the file named SPECIAL.C on  your  monitor.   The 
  631.         last  statement  simply  exits the program and  returns  the 
  632.         value  of 4 to DOS.   Any number from 0 to 9 can be used  in 
  633.         the parentheses for DOS communication.  If you are operating 
  634.         in  a  BATCH  file,  this  number can  be  tested  with  the 
  635.         "ERRORLEVEL" command.
  636.  
  637.              Most compilers that operate in several passes return  a 
  638.         1  with  this mechanism to indicate that a fatal  error  has 
  639.         occurred and it would be a waste of time to go on to another 
  640.         pass resulting in even more errors.
  641.  
  642.              It  is therefore wise to use a batch file for compiling 
  643.         programs and testing the returned value for errors.  A check 
  644.         of  the documentation for my COMPAQ,  resulted in a  minimal 
  645.         and confusing documentation of the "errorlevel" command,  so 
  646.         a brief description of it is given in this file.
  647.  
  648.  
  649.  
  650.  
  651.                                   Page 64
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.                      Chapter 9 - Standard Input/Output
  662.  
  663.  
  664.         PROGRAMMING EXERCISE
  665.  
  666.         1.   Write  a program to  read in a character using a  loop, 
  667.              and  display the character in its normal  "char"  form.      
  668.              Also  display  it  as a decimal  number.  Check  for  a      
  669.              dollar  sign  to use as the  stop  character.  Use  the      
  670.              "getch" form of input so it will print immediately. Hit 
  671.              some of the special keys,  such as function keys,  when 
  672.              you  run the program for some surprises.  You will  get 
  673.              two  inputs  from the special keys,  the first being  a 
  674.              zero  which  is  the indication to the  system  that  a 
  675.              special key was hit.
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.                                   Page 65
  718.  
  719.